package com.ruoyi.labdanger.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.labdanger.domain.LabApplyOccupation;
import com.ruoyi.labdanger.mapper.LabApplyOccupationMapper;
import com.ruoyi.labdanger.service.ILabApplyOccupationService;
import com.ruoyi.labdata.domain.LabFlowInstance;
import com.ruoyi.labdata.service.ILabFlowInstanceService;

/**
 * 申请领用Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-09
 */
@Service
public class LabApplyOccupationServiceImpl implements ILabApplyOccupationService 
{
    @Autowired
    private LabApplyOccupationMapper labApplyOccupationMapper;
    @Autowired
    private ILabFlowInstanceService labFlowInstanceService;

    /**
     * 查询申请领用
     * 
     * @param instanceId 申请领用ID
     * @return 申请领用
     */
    @Override
    public LabApplyOccupation selectLabApplyOccupationById(Long instanceId)
    {
        LabApplyOccupation labApplyOccupation = labApplyOccupationMapper.selectLabApplyOccupationById(instanceId);
        LabFlowInstance labFlowInstance = labFlowInstanceService.selectLabFlowInstanceById(instanceId);
        labApplyOccupation.setFlowName(labFlowInstance.getFlowName());
        labApplyOccupation.setCopyToUsers(labFlowInstance.getCopyToUsers());
        labApplyOccupation.setFlowStatusOpt(labFlowInstance.getFlowStatusOpt());
        return labApplyOccupation;
    }

    /**
     * 查询申请领用列表
     * 
     * @param labApplyOccupation 申请领用
     * @return 申请领用
     */
    @Override
    public List<LabApplyOccupation> selectLabApplyOccupationList(LabApplyOccupation labApplyOccupation)
    {
        SysUser loginUser = SecurityUtils.getLoginUser().getUser();
        labApplyOccupation.setCreateBy(loginUser.getUserName());
        return labApplyOccupationMapper.selectLabApplyOccupationList(labApplyOccupation);
    }

    /**
     * 新增申请领用
     * 
     * @param labApplyOccupation 申请领用
     * @return 结果
     */
    @Override
    public int insertLabApplyOccupation(LabApplyOccupation labApplyOccupation)
    {
        labApplyOccupation.setCreateBy(SecurityUtils.getUsername());
        labApplyOccupation.setCreateTime(DateUtils.getNowDate());
        labApplyOccupation.setCreateOrgId(SecurityUtils.getLoginUser().getUser().getDeptId());
        labFlowInstanceService.insertLabFlowInstance(labApplyOccupation);
        return labApplyOccupationMapper.insertLabApplyOccupation(labApplyOccupation);
    }

    /**
     * 修改申请领用
     * 
     * @param labApplyOccupation 申请领用
     * @return 结果
     */
    @Override
    @Transactional
    public int updateLabApplyOccupation(LabApplyOccupation labApplyOccupation)
    {
        labFlowInstanceService.updateLabFlowInstance(labApplyOccupation);
        return labApplyOccupationMapper.updateLabApplyOccupation(labApplyOccupation);
    }

    /**
     * 批量删除申请领用
     * 
     * @param instanceIds 需要删除的申请领用ID
     * @return 结果
     */
    @Override
    public int deleteLabApplyInstanceIds(Long[] instanceIds)
    {
        return labApplyOccupationMapper.deleteLabApplyInstanceIds(instanceIds);
    }

    /**
     * 删除申请领用信息
     * 
     * @param instanceId 申请领用ID
     * @return 结果
     */
    @Override
    public int deleteLabApplyInstanceId(Long instanceId)
    {
        return labApplyOccupationMapper.deleteLabApplyInstanceId(instanceId);
    }

    @Override
    @Transactional
    public int submitLabApplyOccupation(LabApplyOccupation labApplyOccupation) {
        this.updateLabApplyOccupation(labApplyOccupation);
        return labFlowInstanceService.commitFlow(labApplyOccupation);
    }
}
